const service = require('../../utils/service.js')
const api = require('../../utils/util.js')
Page({
  data: {
    shopData: {},
    userInfo: {},
    shopInfo: {},
    classfiylist: [],
    showEdit: false,
    showMyModel: false,
    newName: '',
    showMyModelE: false,
    editName: '',
    editId: '',
    searchVal: ''
  },
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo')
    this.setData({
      userInfo: userInfo
    })
    this.init()
  },

  init: function () {
    this.getShopInfo(() => {
      this.getList()
    })
  },

  // 获取店铺信息
  getShopInfo: function (cb) {
    let that = this;
    let shopData = wx.getStorageSync('shop_data_shop');
    api.ajax(service.url('getShopDetail'), {
      userid: wx.getStorageSync('out_user_id')
    }, function (res) {
      that.setData({
        shopData: shopData,
        shopInfo: res.data
      })
      cb()
    })
  },

  // 获取分类列表
  getList: function () {
    let that = this
    api.ajax(service.url('categoryGetAll'), {
      unionid: this.data.shopData.shop_unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      shop_code: that.data.shopInfo.shop_code
    }, function (res) {
      if (res.code == 0) {
        that.setData({
          classfiylist: res.data
        })
      } else {
        api.toast(res.messagef)
      }
    })
  },

  editShow: function () {
    this.setData({
      showEdit: true
    })
  },
  editHide: function () {
    this.setData({
      showEdit: false
    })
  },


  // 删除分类
  del: function (e) {
    let id = e.currentTarget.dataset.id
    let that = this
    api.ajax(service.url('categoryActive'), {
      unionid: this.data.shopData.shop_unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      category_id: id,
      shop_code: this.data.shopInfo.shop_code
    }, function (res) {
      if (res.code == 0) {
        api.toast('操作成功')
        that.getList()
      } else {
        api.toast(res.message)
      }
    })
  },


  // 新建分类
  addClassfiy: function () {
    this.setData({
      showMyModel: true,
      newName: ''
    })
  },

  // 获取新建分类名称
  getClassfiyName: function (e) {
    let val = e.detail.value
    this.setData({
      newName: val
    })
  },

  // 确定新建分类
  sureAddClassfiy: function () {
    let that = this
    let val = this.data.newName
    if (val == '') {
      api.toast('请输入分类名称')
      return
    }
    api.ajax(service.url('categoryAdd'), {
      unionid: this.data.shopData.shop_unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      name: val,
      pid: 0,
      shop_code: this.data.shopInfo.shop_code
    }, (res) => {
      if (res.code == 0) {
        this.setData({
          showMyModel: false
        })
        api.toast('新建分类成功')
        this.getList()
      } else {
        api.toast(res.message)
      }
    })
  },

  hideMyModel: function () {
    this.setData({
      showMyModel: false
    })
  },


  // 编辑分类
  edit: function (e) {
    let id = e.currentTarget.dataset.id
    this.setData({
      editId: id,
      showMyModelE: true
    })
  },

  hideMyModelE: function () {
    this.setData({
      showMyModelE: false
    })
  },

  // 获取编辑分类名字
  getClassfiyNameEdit: function (e) {
    let val = e.detail.value
    this.setData({
      editName: val
    })
  },

  // 确认编辑
  sureEditClassfiy: function () {
    let that = this
    api.ajax(service.url('categoryEdit'), {
      unionid: this.data.shopData.shop_unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      category_id: this.data.editId,
      name: this.data.editName,
      shop_code: this.data.shopInfo.shop_code
    }, function (res) {
      if (res.code == 0) {
        api.toast('操作成功')
        that.setData({
          showMyModelE: false
        })
        that.getList()
      } else {
        api.toast(res.message)
      }
    })
  },

  // 获取搜索框内容
  getSearchVal: function (e) {
    let val = e.detail.value
    this.setData({
      searchVal: val
    })
  },

  // 搜索分类
  searchClassfiy: function () {
    let that = this
    api.ajax(service.url('categoryGetAll'), {
      unionid: this.data.shopData.shop_unionid,
      token: this.data.shopData.token,
      token_key: this.data.shopData.token_key,
      shop_code: this.data.shopInfo.shop_code,
      keyword: this.data.searchVal
    }, function (res) {
      if (res.code == 0) {
        that.setData({
          classfiylist: res.data
        })
      } else {
        api.toast(res.message)
      }
    })
  },

  onReachBottom: function () {

  }
})